<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class News extends MY_Controller {
     var $_templates=array();
     var $_lang = '';
    function __construct() {
        parent::__construct();
        $this->session->set_userdata(array('Url'=>uri_string()));
        $this->_lang = $this->lang->lang();
        $this->lang->load('news');
        $this->load->model('news_model','news');
        $this->load->model('buzz_model','buzz');
		$this->load->model('ask_model','ask');
		$this->load->model('comment_model','comment');
		$this->load->helper('like') ;
		$this->load->library('string_library');
		$this->load->library('securimage');
        $this->news->initialise($this->_lang);
		$this->ask->initialise($this->_lang);
		$_GET['active_menu'] = 'tuvan';
    }
	public function index()
	{
      
	}
	function tintuc($alias_cate,$page=0){
		$data=array();
        
		$config['base_url'] = site_url('news/tintuc/'.$alias_cate.'/');
        $data['num']  =  $this->news->getNumNewsByCat($alias_cate);
		$config['total_rows']= $data['num'] ;
		$config['per_page']  =   5;
		$config['uri_segment'] = $this->uri->total_segments();
		$this->pagination->initialize($config);
        $data['pagination']    = $this->pagination->create_links();
         
		$data['listNews'] = $this->news->getAllNewsByCat($alias_cate,$config['per_page'],(int)$page);
		// page title
		$data['title_page'] =$data['listNews'][0]->name; 
		// cac tin khac
		$data['orther_listnews'] = $this->news->getOtherListNews($alias_cate,10,5);
		// nhung cau hoi thuong gap
        //buzz du lịch
        $data['buzzItems']=$this->buzz->get_all_buzz();
		$data['askItems'] = $this->ask->getAskRight();
		$this->_templates['page'] = 'consultative_view';
		$this->site_lib->load($this->_templates['page'],$data);	
	}
	function detail($alias_cate,$alias_new,$id,$page=0){
		$data=array();
		$data['new_detail']=$this->news->getNewById($id);
        if($data['new_detail']){
            $nb_read = $data['new_detail']->nb_read+1;
            $this->news->updateNews($id,array('nb_read'=>$nb_read));
        }
		$data['hot_news']=$this->news->getThreeHotNews($data['new_detail']->category_news_id,$id);
		// load danh sach comment
		$config['base_url'] = site_url('news/comment/'.$id);
        $data['num']  =  $this->comment->get_num_data($id);
		$config['total_rows']= $data['num'] ;
		$config['per_page']  =   5;
		$config['uri_segment'] = $this->uri->total_segments()-1;
		$this->pagination->initialize($config);
        $data['pagination']    = $this->pagination->create_links();
		$data['commentItems'] = $this->comment->getCommentsFromNewId($id,$config['per_page'],(int)$page);
		// độc đáo Việt Nam
		// cac tin khac
		$alias_docdao = $this->string_library->stringchange(lang('news.docdaovn'));
		$data['docdao_vn'] = $this->news->getOtherListNews($alias_docdao,3,0);
		// end
		$data['other_news']=$this->news->getOtherNews($data['new_detail']->category_news_id,$id);
		// page title
		$data['title_page'] =$data['new_detail']->name.' - '.$data['new_detail']->title; 
		
        $this->_templates['page'] = 'newdetail_view';
        $this->site_lib->load($this->_templates['page'],$data);
	}
	
	  function save_comment($news_id)
    {
        $data['success']=true;
        $this->form_validation->set_rules('fullname',lang('news.fullname'),'trim|required');
        $this->form_validation->set_rules('email',lang('news.email'),'trim|required|valid_email');
        $this->form_validation->set_rules('content',lang('news.content'),'trim|required');
            
        if($this->form_validation->run() == FALSE){
           $this->pre_message = validation_errors();
           $data['str_respon'] = $this->pre_message;
           $data['success']=false;
        }else{
            $code = $this->input->post('code');
            if(!$this->securimage->check($code)){
                $data['str_respon'] = lang('news.captcha_incorrect');
                $data['success'] = false;
                
            }else{
                $data_comment=$this->data_comment();
                $id_comment = $this->comment->saveData(0,$data_comment);
               if($id_comment){  
                    $data['success']=true;
                    $data['str_respon']='';
                }else{
                    $data['success']=false;
                    $data['str_respon']='';
                }
           }
        }
        echo json_encode($data); 
    }
	
	
	function update_like($news_id,$comment_id){
		$data=array();
		$data['commentObject'] = $this->comment->get_object($comment_id);
		if($data['commentObject']){
			$like = $data['commentObject']->like+1;
			$update_ok = $this->comment->updateComment($comment_id,array('like'=>$like));
			if($update_ok)
				$data['like']=$like;
			else
				$data['like']=$data['commentObject']->like;
		echo $this->load->view('like_view',$data,true);	
		}
		
	}
	function update_like_news($news_id){
		$data=array();
		$data['commentObject'] = $this->news->getNewById($news_id);
		if($data['commentObject']){
			$like = $data['commentObject']->like+1;
			$update_ok = $this->news->updateNews($news_id,array('like'=>$like));
			echo $like.' ';	
		}
		
	}
	private function data_comment()
    {
        $arr_data = array();
        $arr_data['author']=$this->input->post('fullname');
        $arr_data['author_email']=$this->input->post('email');
        $arr_data['content']=$this->input->post('content');
		$arr_data['object_id']=$this->input->post('news_id');
        $arr_data['dt_create']=date("Y-m-d H:i:s");
		$arr_data['bl_active']=1;
        return $arr_data;
    }
	function comment($news_id,$page=0){
		$data=array();
		// load danh sach comment
		$config['base_url'] = site_url('news/comment/'.$news_id);
        $data['num']  =  $this->comment->get_num_data($news_id);
		$config['total_rows']= $data['num'] ;
		$config['per_page']  =   5;
		$config['uri_segment'] = $this->uri->total_segments()-1;
		$this->pagination->initialize($config);
        $data['pagination']    = $this->pagination->create_links();
		$data['commentItems'] = $this->comment->getCommentsFromNewId($news_id,$config['per_page'],(int)$page);
		$data['new_detail']=$this->news->getNewById($news_id);
		echo $this->load->view('comment_ajax_view',$data,true);	
	}
	
	//update like cho cac mau tin
	function update_num_like($record_id,$ses_name){
		if(!isset($_SESSION[$ses_name])){
			$update_ok = update_like("aw_".$ses_name,$ses_name."_id",$record_id);
			if($update_ok){
				$_SESSION[$ses_name]=array();
				array_push($_SESSION[$ses_name],$record_id);
				$status = $update_ok.' ';
					echo json_encode(
						array(
							'status'=>$status
							)
						);
					exit();
				
			}
		}else if(in_array($record_id,$_SESSION[$ses_name])==false){
				$update_ok = update_like("aw_".$ses_name,$ses_name."_id",$record_id);
				if($update_ok){
					array_push($_SESSION[$ses_name],$record_id);
					$status = $update_ok.' ';
					echo json_encode(
						array(
							'status'=>$status
							)
						);
					exit();
				}
		}else{
			$data['dataObject'] = getObjectLike($ses_name,$record_id);
			$like_old = $data['dataObject']->like;
			echo json_encode(
							array(
									'status'=>false,
									'like_old'=>$like_old,
									'mess'=>lang('anw.liked')
								)
							);
		}
	}
	// end
	
	
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */